package com.by.service.Impl;

import com.by.bean.FreightEnquiry;
import com.by.common.bean.PageBean;
import com.by.common.crud.BaseServiceImpl;
import com.by.common.utils.Assert;
import com.by.mapper.FreightEnquiryDetailMapper;
import com.by.mapper.FreightEnquiryMapper;
import com.by.mapper.FreightOfferMapper;
import com.by.query.FreightCleintQuery;
import com.by.service.FreightEnquiryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FreightEnquiryServiceImpl extends BaseServiceImpl<FreightEnquiry, FreightEnquiryMapper> implements FreightEnquiryService {
    @Autowired
    FreightEnquiryDetailMapper freightEnquiryDetailMapper;

    @Autowired
    FreightOfferMapper freightOfferMapper;

    public int save(FreightEnquiry entity) {
        //生成编码
        //保存货代询价
        int rs = super.save(entity);
        Assert.isNotSuccess(rs, "保存货代询价单失败");
        //保存明细
        if (null != entity.getFreightEnquiryDetailList() && entity.getFreightEnquiryDetailList().size() > 0) {
            //批量保存
            rs = freightEnquiryDetailMapper.batchSave(entity.getId(), entity.getFreightEnquiryDetailList());
            Assert.isNotSuccess(rs, "保存货代询价单明细失败");
        }

        //根据运输方式和航线 保存 货代 相关信息
        rs = freightOfferMapper.saveFreight(entity.getId(), entity.getTransportway(), entity.getAirline());
        Assert.isNotSuccess(rs, "保存能够报价的货代失败");


        return 1;
    }

    @Override
    public PageBean queryFreight(FreightCleintQuery query) {
        PageHelper.startPage(query.getPage(),query.getPageSize());
        List list=baseMapper.queryFreight(query);
        PageInfo info=new PageInfo(list);
        return new PageBean(query.getPage(),query.getPageSize(),info.getTotal(),list);
    }
}
